import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
import './controls'

Window {
    id:window
    width: 375
    height: 640
    visible: true
    title: '水上乐园管理系统'
    flags: Qt.Window | Qt.FramelessWindowHint
    visibility: Window.Maximized
    minimumHeight: 640
    minimumWidth: 375
    signal refreshData()
    Component.onCompleted: {
        CGlobal.window=this
    }
    onClosing: function(closeevent){
        if(!CommonHelper.question(window,qsTr('确定要退出吗?')))
        {
            closeevent.accepted = false
            return
        }
//        window.destroy()
    }

    Gradient{
        id:mainGradient
        GradientStop{ position: 0.0; color: '#4FB0C8'}
        GradientStop{ position: 1.0; color: '#815C5B'}
    }

    property int pageCount:0
    function addPage(url,properties){
        var component = Qt.createComponent( url );
        if ( component.status === Component.Ready )
        {
            if(properties)
            {
                var result = component.createObject( pages,properties );
            }
            else
            {
                var result = component.createObject( pages, {} );
            }
            pageCount= pages.children.length
        }
    }

    function goBack(){
        if(pages.children.length > 0 )
        {
            pages.children[pages.children.length - 1].destroy();
            pageCount--
            if(pageCount === 0)
            {
                refreshData()
            }
        }
    }

    function clearAll()
    {
        for(let i=0;i< pages.children.length ;i++)
        {
            pages.children[i].destroy();
        }
        pageCount=0;
    }

    Rectangle {
        property string foregroundColor: "#242424"
        property string backgroundColor: "#969696"

        property bool changeColor: true
        property bool active: changeColor?Window.active: true

        anchors.fill: parent

        border.width: 1
        color: "transparent"
        border.color: active ? foregroundColor : backgroundColor

    }

    Rectangle{
        id:titleBar
        width: parent.width
        height: 60
        color: '#1E728D'
        Text {
            anchors.verticalCenter: parent.verticalCenter
            font.pixelSize: 24
            font.bold: true
            text: window.title
            color: '#ffffff'
            x:20
        }
        Text {
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: btnOut.left
            anchors.rightMargin: 20
            font.pixelSize: 16
            text: qsTr("当前用户 : ")+ UserHelper.userinfo.real_name
            color: '#ffffff'
            visible: mainViewLoader.active
        }
        Button{
            id:btnOut
            visible: mainViewLoader.active
            width: 60
            height: 32
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: btnClose.left
            anchors.rightMargin: 40
            background: Rectangle{
                radius: 10
                color: 'transparent' //'#d81e06'
                Text {
                    anchors.centerIn: parent
                    color: '#ffffff'
                    text: qsTr("注销")
                }
            }
            onClicked: {
                if(CommonHelper.question(window,qsTr('确定要退出登录吗?')))
                {
                    clearAll()
                    loginViewLoader.active = true;
                    mainViewLoader.active  = false;
                }
            }
        }
        Button{
            id:btnClose
            width: 30
            height: 30
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 20
            // background: Image{
            //     width: 24
            //     height: 24
            //     source: 'qrc:/images/close2.png'
            //     anchors.centerIn: parent
            // }
            background: Rectangle{
                radius: 10
                color: 'transparent' //'#d81e06'
                Text {
                    anchors.centerIn: parent
                    color: '#ffffff'
                    text: qsTr("退出")
                }
            }
            onClicked: {
                window.close()
            }
        }
    }

    Rectangle{
        anchors.fill: parent
        anchors.topMargin: titleBar.height
        Loader {
            id: loginViewLoader
            anchors.fill: parent
            active: true
            sourceComponent: LoginView {
                onLoginSuccessed: {
                    loginViewLoader.active = false;
                    mainViewLoader.active  = true;
                }
            }
        }
        Loader {
            id: mainViewLoader
            anchors.fill: parent
            active: !loginViewLoader.active
            sourceComponent: MainView {
                id:mainView
            }
        }

        Item {
            id:pages
            anchors.fill: parent
        }

    }

    Button{
        id:btnBack
        width: 32
        height: 32
        visible: pageCount > 0
        anchors.top: titleBar.bottom
        anchors.topMargin: 10
        x:10
        background: Image {
            anchors.centerIn: parent
            source: "qrc:/images/back.png"
        }
        onClicked: {
           window.goBack()
        }
    }


}
